<template>
  <el-dialog class="dialog-mini" width="700px" title="新增/修改" :visible.sync="visible">
    <el-form :rules="rules" ref="dataForm" :model="temp" label-position="right" label-width="100px">
      <el-form-item size="small" label="上级分类" prop="parentId">
        <treeselect ref="modulesTree" :normalizer="normalizer" :disabled="treeDisabled" :options="treeCate" :default-expand-level="3" :multiple="false" :open-on-click="true" :open-on-focus="true" :clear-on-select="true"
          v-model="temp.parentId"></treeselect>
        <div class="form-item-help">
          <div class="extra">请参照分类模板决定分类层级</div>
          <router-link target="_blank" :to="{ path: '/store/CateTemplate/index' }">查看分类模板</router-link>
        </div>
      </el-form-item>
      <el-form-item size="small" label="分类名" prop="name">
        <el-input v-model="temp.name" placeholder="请输入分类名称"></el-input>
      </el-form-item>
      <el-form-item size="small" label="分类图" prop="imageId">
        <imgSelect v-model="listFile" textExplain="只能上传jpg/png文件，高宽相等且大于480px,大小1M内" :limit="1">
        </imgSelect>
      </el-form-item>
      <el-form-item size="small" label="排序" prop="sortNo">
        <el-input-number v-model="temp.sortNo" :min="1" :max="800"></el-input-number>
      </el-form-item>

    </el-form>
    <div slot="footer">
      <el-button size="mini" @click="handleCancel()">取消</el-button>
      <el-button size="mini" type="primary" @click="saveData">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import * as apiGoodsCate from '@/api/goods/goodscate'
import CategoryModel from '@/common/model/Category'
import imgSelect from '@/utils/imgSelect'
import _ from 'lodash'
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  components: {
    Treeselect,
    imgSelect
  },
  data() {
    return {
      visible: false,
      form: this.$form.createForm(this),
      treeCate: [],
      listFile: [],
      temp: {},
      normalizer(node) {
        // treeselect定义字段
        return {
          label: node.title,
          id: node.id,
          children: node.children,
        };
      },
      treeDisabled: false, // 树选择框时候可用
      rules: {
        name: [{ required: true, message: '名称不能为空', trigger: 'blur' }]
      },
    }
  },
  beforeCreate() {

  },
  created() {

  },
  watch: {
    // arrImg: {
    //   //监控listUrlDetail改变
    //   handler() {
    //     this.temp.url = this.arrImg[0];
    //     console.log("watch temp", JSON.stringify(this.temp));
    //   },
    //   deep: true,
    // },
  },
  created() {
    this.listCateData()
  },
  methods: {
    show(record) {
      this.visible = true;
      if (record) {
        // this.temp = record;
        this.getDetail(record.id)
      } else {
        this.listFile = [];
        this.temp = {}
        // 更新form元素
        this.$nextTick(() => {
          this.$forceUpdate();
        });
      }

    },
    //加载数据
    getDetail(id) {
      apiGoodsCate.get({ id: id }).then((res) => {
        this.form.resetFields()
        this.$nextTick(() => {
          this.temp = res.result;
          this.listFile = this.temp.listFile
          console.log("111", JSON.stringify(this.temp.listFile))
        })
      })
    },
    listCateData() {
      CategoryModel.getCategoryTreeSelect().then((list) => {
        const tempTree = list
        // 顶级分类
        this.treeCate = [
          {
            title: '顶级分类',
            key: '',
            value: '',
            id: '',
            label: '顶级分类'
          },
        ].concat(tempTree)

      })
    },
    saveData(e) {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          tempData.listImageId = this.listFile.map(p => p.id)
          console.log("dddd", JSON.stringify(tempData));
          // return;
          apiGoodsCate.addOrUpdate(tempData).then((res) => {
            // 显示成功
            this.$message.success(res.message, 1.5);
            // 关闭对话框事件
            this.handleCancel();
            // 通知父端组件提交完成了
            this.$emit("handleSubmit");
          })
        }
      })
    },
    //  关闭对话框事件
    handleCancel() {
      this.visible = false;
      this.$nextTick(() => {
        this.$forceUpdate();
      });
    },

  },
};
</script>
<style>
</style>
